package com.ruitu.arad.view;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

import com.ruitu.arad.util.BarUtils;
import com.ruitu.arad.util.ScreenUtils;
import com.ruitu.arad.util.SizeUtils;

import androidx.annotation.Nullable;

/**
 * @author martin
 */
public class MoveView extends LinearLayout {

    /**
     * View的宽高
     */
    private float width;
    private float height;

    /**
     * 触摸点相对于View的坐标
     */
    private float touchX;
    private float touchY;
    /**
     * 状态栏的高度
     */
    int barHeight = 0;
    /**
     * 屏幕的宽高
     */
    private int screenWidth;
    private int screenHeight;

    public MoveView(Context context) {
        super(context);
    }

    public MoveView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //获取状态栏高度
        barHeight = BarUtils.getStatusBarHeight(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        screenWidth = ScreenUtils.getScreenWidth();
        screenHeight = ScreenUtils.getScreenHeight();
        width = canvas.getWidth();
        height = canvas.getHeight();
    }

    private int dX, dY, myX, myY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                clearAnimation();
                touchX = event.getX();
                touchY = event.getY();
                myX = (int) event.getRawX();
                myY = (int) event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                /*
                    这里Y轴要减掉barHeight
                    因为计算时，View的默认坐标原点在状态栏下面
                    而屏幕的坐标是包含着状态栏的
                 */
                float nowY = event.getRawY() - touchY - barHeight;//
                float nowX = event.getRawX() - touchX;
                nowX = nowX < 0 ? 0 : (nowX + width > screenWidth) ? (screenWidth - width) : nowX;
                nowY = nowY < 0 ? 0 : nowY + height > screenHeight ? screenHeight - height : nowY;

                int topLimitY = SizeUtils.dp2px(50);//屏幕上限
                if (nowY < topLimitY) {
                    nowY = topLimitY;
                }

                int bottomLimitY = ScreenUtils.getScreenHeight() - SizeUtils.dp2px(120);//屏幕下限
                if (nowY > bottomLimitY) {
                    nowY = bottomLimitY;
                }

                this.setY(nowY);
                this.setX(nowX);
                invalidate();
                return true;
            case MotionEvent.ACTION_UP:
                //这里做动画贴边效果
                float centerX = getX() + width / 2;
                if (centerX > screenWidth / 2) {
                    ObjectAnimator.ofFloat(this, "translationX", getX(), screenWidth - width)
                            .setDuration(300)
                            .start();
                } else {
                    ObjectAnimator.ofFloat(this, "translationX", getX(), 0)
                            .setDuration(300)
                            .start();
                }

                touchX = touchY = 0;

                dX = (int) Math.abs(event.getRawX() - myX);
                dY = (int) Math.abs(event.getRawY() - myY);
//                Logg.i("getRawX() = " + event.getRawX() + "...getRawY() = " + event.getRawY());

                if (dX >= 5 || dY >= 5) {
                    return true;
                } else {
                    return super.onTouchEvent(event);
                }

            default:
                return super.onTouchEvent(event);
//                return true;
        }
        return super.onTouchEvent(event);
    }
}